from rest_framework.decorators import api_view
from rest_framework.generics import GenericAPIView

from utils.network import Result
from .models import Banner, Classify, TabBarImage
from .serializers import BannerSerializer, ClassifySerializer, TabBarImageSerializer, ClassifyTextSerializer


class OnlyGet(GenericAPIView):
    def get(self, requests):
        serializer = self.get_serializer(self.get_queryset(), many=True)
        return Result.success(data=serializer.data)

    def post(self, requests):
        data = requests.data
        serializer = self.serializer(data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Result.success()


class BannerView(OnlyGet):
    """首页轮播图，没有参数"""
    queryset = Banner.objects.all()
    serializer_class = BannerSerializer


class LabelView(OnlyGet):
    """分类标签"""
    queryset = Classify.objects.all()
    serializer_class = ClassifySerializer


class TabBarImageView(OnlyGet):
    """页脚图片，没有参数"""
    queryset = TabBarImage.objects.all()
    serializer_class = TabBarImageSerializer


class ClassifyView(OnlyGet):
    queryset = Classify.objects.values('id', 'name').all()
    serializer_class = ClassifyTextSerializer
